package com.util;

import java.security.SecureRandom;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.locks.ReentrantLock;

public class OrderNumberGenerator {

	private static final ReentrantLock lock = new ReentrantLock();

	/**
	 * 
	 * @param prefix 前缀
	 * @return
	 */
	public static String generateOrderNumber(String prefix) {
		// 获取当前日期并格式化为“年月日”
		try {
			lock.lock();

			DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
			String datePart = LocalDate.now().format(formatter);

			// 生成6位随机数
			SecureRandom random = new SecureRandom();
			String randomPart = String.format("%06d", random.nextInt(900000) + 10000); // 保证是6位且不为0开头
			lock.unlock();
			// 组合订单号：前缀“ZF” + 日期 + 随机数
			return prefix + datePart + randomPart;
		} finally {
			if (lock.isLocked()) {
				lock.unlock();
			}
		}
	}
}